package com.erp.erp_ui.basic.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.erp.enums.DictionaryLxEnum;
import com.erp.enums.DictionaryTypeEnum;
import com.erp.erp_entitys.basic.entity.DictionaryEntity;
import com.erp.erp_entitys.warehouse.req.ProductInfoReq;
import com.erp.erp_entitys.warehouse.rsp.ProductInfoRsp;
import com.erp.erp_servers.basic.IDictionaryService;
import com.erp.erp_servers.warehouse.IStockService;
import com.erp.erp_ui.basic.req.AddDictionaryReq;
import com.erp.erp_ui.basic.vo.DictionaryCategoryVo;
import com.erp.erp_ui.basic.vo.DictionaryPageListVo;
import com.erp.erp_ui.warehouse.enums.DataIsValidEnum;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName : ProductService
 * @Description : 产品业务逻辑层
 * @Author : lst
 * @Date: 2021-09-14 00:07
 */
@Service
public class ProductService {

    /**
     * 日志
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(ProductService.class);

    /**
     * 字典接口
     */
    private final IDictionaryService dictionaryService;

    /**
     * 库存接口
     */
    private final IStockService stockService;

    @Autowired
    public ProductService(IDictionaryService dictionaryService,
                          IStockService stockService) {
        this.dictionaryService = dictionaryService;
        this.stockService = stockService;
    }

    /**
     * 获取分页列表
     * @param lx 小类
     * @param name 名称
     * @return 分页列表
     */
    public PageInfo<DictionaryPageListVo> getDictionaryListByPage(Integer lx, String name) {
        //初始化回参对象集合
        List<DictionaryPageListVo> voList = new ArrayList<>();
        if (null == lx) {
            PageInfo pageInfo = new PageInfo();
            pageInfo.setList(voList);
            return pageInfo;
        }
        DictionaryEntity req = new DictionaryEntity();
        req.setType(DictionaryTypeEnum.COMMODITY_INFO.getId());
        req.setLx(lx);
        req.setName(name);
        //调用接口查询结果
        List<DictionaryEntity> list = dictionaryService.getDictionaryList(req);
        PageInfo pageInfo = new PageInfo(list);
        if (CollectionUtils.isEmpty(list)) {
            pageInfo.setList(voList);
            return pageInfo;
        }
        for (DictionaryEntity info : list) {
            DictionaryPageListVo vo = new DictionaryPageListVo();
            //赋值
            BeanUtils.copyProperties(info, vo);

            voList.add(vo);
        }

        pageInfo.setList(voList);
        return pageInfo;
    }

    /**
     * 品类新增、修改
     * @param req 品类新增、修改入参
     * @return 品类新增、修改结果
     */
    @Transactional(rollbackFor = Exception.class)
    public R addCategory(AddDictionaryReq req) {
        //一：编码唯一性校验
        boolean codeCheckFlag = (null != req.getId());
        if (!codeCheckFlag) {
            //编码做唯一校验
            boolean codeFlag = checkRepeatCode(req);
            if (!codeFlag) {
                return R.buildError(ResultStatusEnum.DICTIONARY_CODE_REPEAT);
            }

            //名称做唯一校验
            boolean nameFlag = checkRepeatName(req);
            if (!nameFlag) {
                return R.buildError(ResultStatusEnum.DICTIONARY_NAME_REPEAT);
            }

            //中文描述做唯一校验
            boolean chineseDescFlag = checkRepeatChineseDesc(req);
            if (!chineseDescFlag) {
                return R.buildError(ResultStatusEnum.DICTIONARY_CHINESE_DESCRIPTION_REPEAT);
            }

            //英文描述做唯一校验
            boolean englishDescFlag = checkRepeatEnglishDesc(req);
            if (!englishDescFlag) {
                return R.buildError(ResultStatusEnum.DICTIONARY_ENGLISH_DESCRIPTION_REPEAT);
            }
        }else {
            //获取修改前数据
            DictionaryEntity oldDictionary = dictionaryService.queryById(req.getId());

            //修改时：如果新的名称不为空且和修改前名称不一致，则需要进行校验
            if(StringUtils.isNotEmpty(req.getName()) && !req.getName().equals(oldDictionary.getName())){
                //名称做唯一校验
                boolean nameFlag = checkRepeatName(req);
                if (!nameFlag) {
                    return R.buildError(ResultStatusEnum.DICTIONARY_NAME_REPEAT);
                }
            }

            //修改时：如果新的中文描述不为空且和修改前中文描述不一致，则需要进行校验
            if(StringUtils.isNotEmpty(req.getRemark()) && !req.getRemark().equals(oldDictionary.getRemark())){
                //中文描述做唯一校验
                boolean chineseDescFlag = checkRepeatChineseDesc(req);
                if (!chineseDescFlag) {
                    return R.buildError(ResultStatusEnum.DICTIONARY_CHINESE_DESCRIPTION_REPEAT);
                }
            }

            //修改时：如果新的英文描述不为空且和修改前英文描述不一致，则需要进行校验
            if(StringUtils.isNotEmpty(req.getEnglishname()) && !req.getEnglishname().equals(oldDictionary.getEnglishname())){
                //英文描述做唯一校验
                boolean englishDescFlag = checkRepeatEnglishDesc(req);
                if (!englishDescFlag) {
                    return R.buildError(ResultStatusEnum.DICTIONARY_ENGLISH_DESCRIPTION_REPEAT);
                }
            }
        }

        //二：新增、修改
        //初始化对象
        DictionaryEntity dictionary = new DictionaryEntity();
        //赋值
        BeanUtils.copyProperties(req, dictionary);
        if (null == req.getId()) {
            //新增

            //字典大类
            dictionary.setType(DictionaryTypeEnum.COMMODITY_INFO.getId());
            //字典小类
            dictionary.setLx(req.getLx());

            dictionaryService.insert(dictionary);
        } else {
            //修改
            dictionaryService.update(dictionary);
        }

        return R.build(ResultStatusEnum.SUCCESS);
    }

    /**
     * 校验编码是否存在
     * @return true:不重复 false:重复
     */
    private boolean checkRepeatCode(AddDictionaryReq dictionaryReq){
        DictionaryEntity req = new DictionaryEntity();
        req.setCode(dictionaryReq.getCode());
        req.setType(DictionaryTypeEnum.COMMODITY_INFO.getId());
        req.setLx(dictionaryReq.getLx());
        List<DictionaryEntity> dictionaryList = dictionaryService.queryAll(req);
        if(CollectionUtils.isEmpty(dictionaryList)){
            return true;
        }
        return false;
    }

    /**
     * 校验名称是否存在
     * @return true:不重复 false:重复
     */
    private boolean checkRepeatName(AddDictionaryReq dictionaryReq){
        if(StringUtils.isEmpty(dictionaryReq.getName())){
            return true;
        }
        DictionaryEntity req = new DictionaryEntity();
        req.setType(DictionaryTypeEnum.COMMODITY_INFO.getId());
        req.setLx(dictionaryReq.getLx());
        req.setName(dictionaryReq.getName());
        List<DictionaryEntity> dictionaryList = dictionaryService.queryAll(req);
        if(CollectionUtils.isEmpty(dictionaryList)){
            return true;
        }
        return false;
    }

    /**
     * 校验中文描述是否存在
     * @return true:不重复 false:重复
     */
    private boolean checkRepeatChineseDesc(AddDictionaryReq dictionaryReq){
        if(StringUtils.isEmpty(dictionaryReq.getRemark())){
            return true;
        }
        DictionaryEntity req = new DictionaryEntity();
        req.setType(DictionaryTypeEnum.COMMODITY_INFO.getId());
        req.setLx(dictionaryReq.getLx());
        req.setRemark(dictionaryReq.getRemark());
        List<DictionaryEntity> dictionaryList = dictionaryService.queryAll(req);
        if(CollectionUtils.isEmpty(dictionaryList)){
            return true;
        }
        return false;
    }

    /**
     * 校验英文描述是否存在
     * @return true:不重复 false:重复
     */
    private boolean checkRepeatEnglishDesc(AddDictionaryReq dictionaryReq){
        if(StringUtils.isEmpty(dictionaryReq.getEnglishname())){
            return true;
        }
        DictionaryEntity req = new DictionaryEntity();
        req.setType(DictionaryTypeEnum.COMMODITY_INFO.getId());
        req.setLx(dictionaryReq.getLx());
        req.setEnglishname(dictionaryReq.getEnglishname());
        List<DictionaryEntity> dictionaryList = dictionaryService.queryAll(req);
        if(CollectionUtils.isEmpty(dictionaryList)){
            return true;
        }
        return false;
    }

    /**
     * 品类批量删除
     * @param ids 删除的品类id集合
     */
    public void categoryBatchDelete(String ids){
        if (StringUtils.isBlank(ids)) {
            return;
        }
        //多个id以英文逗号隔开
        List<String> newIds = Arrays.asList(ids.split(","));
        for(String id : newIds){
            //初始化对象
            DictionaryEntity req = new DictionaryEntity();
            //设置修改主键
            req.setId(Integer.parseInt(id));
            //数据设为无效
            req.setValid(DataIsValidEnum.INVALID.getId());
            //修改数据
            dictionaryService.update(req);
        }
    }

    /**
     * 获取最新字典编码
     * @param lx 小类
     * @return 最新字典编码
     */
    public String getNewDictionaryCode(Integer lx){
        if(null == lx){
            return null;
        }
        //初始化查询对象
        DictionaryEntity req = new DictionaryEntity();
        //大类
        req.setType(DictionaryTypeEnum.COMMODITY_INFO.getId());
        //小类
        req.setLx(lx);
        String prefix = "";
        switch (lx){
            case 61:
                prefix = "CG-";
                break;
            case 62:
                prefix = "MCG-";
                break;
            case 63:
                prefix = "FCG-";
                break;
            case 64:
                prefix = "SCG-";
                break;
            case 65:
                prefix = "TCG-";
                break;
            case 66:
                prefix = "CF-";
                break;
            case 67:
                prefix = "CON-";
                break;
            case 68:
                prefix = "GTC-";
                break;
            case 69:
                prefix = "GTS-";
                break;
            case 70:
                prefix = "SES-";
                break;
            case 71:
                prefix = "GPS-";
                break;
            case 72:
                prefix = "GSS-";
                break;
            case 73:
                prefix = "MCS-";
                break;
            case 74:
                prefix = "PMN-";
                break;
            case 75:
                prefix = "OSS-";
                break;
            case 76:
                prefix = "OSP-";
                break;
            case 77:
                prefix = "OCS-";
                break;
            case 78:
                prefix = "OCN-";
                break;
            case 79:
                prefix = "PCG-";
                break;
            case 80:
                prefix = "PMC-";
                break;
            case 81:
                prefix = "PFC-";
                break;
            case 82:
                prefix = "PSC-";
                break;
            case 83:
                prefix = "PTC-";
                break;
            case 84:
                prefix = "PCF-";
                break;
            case 85:
                prefix = "PCO-";
                break;
            case 86:
                prefix = "PPM-";
                break;
            case 88:
                prefix = "GKS-";
                break;
            case 89:
                prefix = "OTS-";
                break;
            case 90:
                prefix = "PAS-";
                break;
            case 91:
                prefix = "SPN-";
                break;
        }
        //获取字典编码
        List<DictionaryEntity> dictionaryCodes = dictionaryService.getDictionaryCodeByQuery(req);
        if(CollectionUtils.isEmpty(dictionaryCodes)){
            //未创建单据，单号从001开始
            return prefix + "001";
        }
        //已创建编码,获取最后创建的编码
        String code = dictionaryCodes.get(0).getCode();
        //截取最后三位序列号
        String serialNumber = code.substring(code.length()-3);
        //数据转换
        BigDecimal formatBigDecimal = new BigDecimal(serialNumber);
        //生成新的序列号(序列号自增一位)
        String newSerialNumber = formatBigDecimal.add(new BigDecimal("1")).toString();
        if(1 == newSerialNumber.length()){
            newSerialNumber = "00" + newSerialNumber;
        }else if(2 == newSerialNumber.length()){
            newSerialNumber = "0" + newSerialNumber;
        }
        //生成最新编码
        return prefix + newSerialNumber;
    }

    /**
     * 获取模具字典信息
     * @return 模具字典信息
     */
    public DictionaryCategoryVo getDictionaryCategory(){
        //初始化查询对象
        DictionaryEntity req = new DictionaryEntity();
        //大类
        req.setType(DictionaryTypeEnum.COMMODITY_INFO.getId());
        //数据有效
        req.setValid(DataIsValidEnum.VALID.getId());
        //查询结果
        List<DictionaryEntity> dictionaryList = dictionaryService.queryAll(req);
        //初始化回参对象
        DictionaryCategoryVo vo = new DictionaryCategoryVo();
        if(CollectionUtils.isEmpty(dictionaryList)){
            return vo;
        }
        //品类
        List<DictionaryEntity> categoryList = new ArrayList<>();

        //模具类别
        List<DictionaryEntity> moldCategoryList = new ArrayList<>();

        //一级类目
        List<DictionaryEntity> firstCategoryList = new ArrayList<>();

        //二级类目
        List<DictionaryEntity> secondaryCategoryList = new ArrayList<>();

        //三级类目
        List<DictionaryEntity> threeLevelCategoryList = new ArrayList<>();

        //工艺
        List<DictionaryEntity> craftList = new ArrayList<>();

        //颜色订货号
        List<DictionaryEntity> colorOrderNumberList = new ArrayList<>();

        //私模订货号
        List<DictionaryEntity> privateMoldNumberList = new ArrayList<>();

        //规格尺寸(其它)
        List<DictionaryEntity> otherSpecificationSizeList = new ArrayList<>();

        //特殊参数(其它)
        List<DictionaryEntity> otherSpecialParametersList = new ArrayList<>();

        //品类(其它)
        List<DictionaryEntity> otherCategoryList = new ArrayList<>();

        //客户货号(其它)
        List<DictionaryEntity> otherCustomerNumberList = new ArrayList<>();

        //特殊需求
        List<DictionaryEntity> specialNeedsList = new ArrayList<>();

        for(DictionaryEntity dictionary : dictionaryList){
            Integer lx = dictionary.getLx();
            if(null == lx){
                continue;
            }
            switch (lx){
                case 61:
                    categoryList.add(dictionary);
                    break;
                case 62:
                    moldCategoryList.add(dictionary);
                    break;
                case 63:
                    firstCategoryList.add(dictionary);
                    break;
                case 64:
                    secondaryCategoryList.add(dictionary);
                    break;
                case 65:
                    threeLevelCategoryList.add(dictionary);
                    break;
                case 66:
                    craftList.add(dictionary);
                    break;
                case 67:
                    colorOrderNumberList.add(dictionary);
                    break;
                case 74:
                    privateMoldNumberList.add(dictionary);
                    break;
                case 75:
                    otherSpecificationSizeList.add(dictionary);
                    break;
                case 76:
                    otherSpecialParametersList.add(dictionary);
                    break;
                case 77:
                    otherCategoryList.add(dictionary);
                    break;
                case 78:
                    otherCustomerNumberList.add(dictionary);
                    break;
                case 89:
                    specialNeedsList.add(dictionary);
                    break;
            }
        }

        vo.setCategoryList(categoryList);
        vo.setMoldCategoryList(moldCategoryList);
        vo.setFirstCategoryList(firstCategoryList);
        vo.setSecondaryCategoryList(secondaryCategoryList);
        vo.setThreeLevelCategoryList(threeLevelCategoryList);
        vo.setCraftList(craftList);
        vo.setColorOrderNumberList(colorOrderNumberList);
        vo.setPrivateMoldNumberList(privateMoldNumberList);
        vo.setOtherSpecificationSizeList(otherSpecificationSizeList);
        vo.setOtherSpecialParametersList(otherSpecialParametersList);
        vo.setOtherCategoryList(otherCategoryList);
        vo.setOtherCustomerNumberList(otherCustomerNumberList);
        vo.setSpecialNeedsList(specialNeedsList);

        return vo;
    }

    /**
     * 获取模具字典包材信息
     * @return 模具字典包材信息
     */
    public DictionaryCategoryVo getDictionaryPackingMaterialCategory(){
        //初始化查询对象
        DictionaryEntity req = new DictionaryEntity();
        //大类
        req.setType(DictionaryTypeEnum.COMMODITY_INFO.getId());
        //数据有效
        req.setValid(DataIsValidEnum.VALID.getId());
        //查询结果
        List<DictionaryEntity> dictionaryList = dictionaryService.queryAll(req);
        //初始化回参对象
        DictionaryCategoryVo vo = new DictionaryCategoryVo();
        if(CollectionUtils.isEmpty(dictionaryList)){
            return vo;
        }
        //品类
        List<DictionaryEntity> categoryList = new ArrayList<>();

        //模具类别
        List<DictionaryEntity> moldCategoryList = new ArrayList<>();

        //一级类目
        List<DictionaryEntity> firstCategoryList = new ArrayList<>();

        //二级类目
        List<DictionaryEntity> secondaryCategoryList = new ArrayList<>();

        //三级类目
        List<DictionaryEntity> threeLevelCategoryList = new ArrayList<>();

        //工艺
        List<DictionaryEntity> craftList = new ArrayList<>();

        //颜色订货号
        List<DictionaryEntity> colorOrderNumberList = new ArrayList<>();

        //私模订货号
        List<DictionaryEntity> privateMoldNumberList = new ArrayList<>();

        //规格尺寸(其它)
        List<DictionaryEntity> otherSpecificationSizeList = new ArrayList<>();

        //特殊参数(其它)
        List<DictionaryEntity> otherSpecialParametersList = new ArrayList<>();

        //品类(其它)
        List<DictionaryEntity> otherCategoryList = new ArrayList<>();

        //客户货号(其它)
        List<DictionaryEntity> otherCustomerNumberList = new ArrayList<>();

        //特殊需求
        List<DictionaryEntity> specialNeedsList = new ArrayList<>();

        for(DictionaryEntity dictionary : dictionaryList){
            Integer lx = dictionary.getLx();
            if(null == lx){
                continue;
            }
            switch (lx){
                case 61:
                    categoryList.add(dictionary);
                    break;
                case 62:
                    moldCategoryList.add(dictionary);
                    break;
                case 63:
                    firstCategoryList.add(dictionary);
                    break;
                case 64:
                    secondaryCategoryList.add(dictionary);
                    break;
                case 65:
                    threeLevelCategoryList.add(dictionary);
                    break;
                case 66:
                    craftList.add(dictionary);
                    break;
                case 67:
                    colorOrderNumberList.add(dictionary);
                    break;
                case 74:
                    privateMoldNumberList.add(dictionary);
                    break;
                case 75:
                    otherSpecificationSizeList.add(dictionary);
                    break;
                case 76:
                    otherSpecialParametersList.add(dictionary);
                    break;
                case 77:
                    otherCategoryList.add(dictionary);
                    break;
                case 78:
                    otherCustomerNumberList.add(dictionary);
                    break;
                case 91:
                    specialNeedsList.add(dictionary);
                    break;
            }
        }

        vo.setCategoryList(categoryList);
        vo.setMoldCategoryList(moldCategoryList);
        vo.setFirstCategoryList(firstCategoryList);
        vo.setSecondaryCategoryList(secondaryCategoryList);
        vo.setThreeLevelCategoryList(threeLevelCategoryList);
        vo.setCraftList(craftList);
        vo.setColorOrderNumberList(colorOrderNumberList);
        vo.setPrivateMoldNumberList(privateMoldNumberList);
        vo.setOtherSpecificationSizeList(otherSpecificationSizeList);
        vo.setOtherSpecialParametersList(otherSpecialParametersList);
        vo.setOtherCategoryList(otherCategoryList);
        vo.setOtherCustomerNumberList(otherCustomerNumberList);
        vo.setSpecialNeedsList(specialNeedsList);

        return vo;
    }

    /**
     * 获取模具字典配件信息
     * @return 模具字典配件信息
     */
    public DictionaryCategoryVo getDictionaryPartsCategory(){
        //初始化查询对象
        DictionaryEntity req = new DictionaryEntity();
        //大类
        req.setType(DictionaryTypeEnum.COMMODITY_INFO.getId());
        //数据有效
        req.setValid(DataIsValidEnum.VALID.getId());
        //查询结果
        List<DictionaryEntity> dictionaryList = dictionaryService.queryAll(req);
        //初始化回参对象
        DictionaryCategoryVo vo = new DictionaryCategoryVo();
        if(CollectionUtils.isEmpty(dictionaryList)){
            return vo;
        }
        //品类
        List<DictionaryEntity> categoryList = new ArrayList<>();

        //模具类别
        List<DictionaryEntity> moldCategoryList = new ArrayList<>();

        //一级类目
        List<DictionaryEntity> firstCategoryList = new ArrayList<>();

        //二级类目
        List<DictionaryEntity> secondaryCategoryList = new ArrayList<>();

        //三级类目
        List<DictionaryEntity> threeLevelCategoryList = new ArrayList<>();

        //工艺
        List<DictionaryEntity> craftList = new ArrayList<>();

        //颜色订货号
        List<DictionaryEntity> colorOrderNumberList = new ArrayList<>();

        //私模订货号
        List<DictionaryEntity> privateMoldNumberList = new ArrayList<>();

        //特殊需求
        List<DictionaryEntity> specialNeedsList = new ArrayList<>();

        for(DictionaryEntity dictionary : dictionaryList){
            Integer lx = dictionary.getLx();
            if(null == lx){
                continue;
            }
            switch (lx){
                case 79:
                    categoryList.add(dictionary);
                    break;
                case 80:
                    moldCategoryList.add(dictionary);
                    break;
                case 81:
                    firstCategoryList.add(dictionary);
                    break;
                case 82:
                    secondaryCategoryList.add(dictionary);
                    break;
                case 83:
                    threeLevelCategoryList.add(dictionary);
                    break;
                case 84:
                    craftList.add(dictionary);
                    break;
                case 85:
                    colorOrderNumberList.add(dictionary);
                    break;
                case 86:
                    privateMoldNumberList.add(dictionary);
                    break;
                case 90:
                    specialNeedsList.add(dictionary);
                    break;
            }
        }

        vo.setCategoryList(categoryList);
        vo.setMoldCategoryList(moldCategoryList);
        vo.setFirstCategoryList(firstCategoryList);
        vo.setSecondaryCategoryList(secondaryCategoryList);
        vo.setThreeLevelCategoryList(threeLevelCategoryList);
        vo.setCraftList(craftList);
        vo.setColorOrderNumberList(colorOrderNumberList);
        vo.setPrivateMoldNumberList(privateMoldNumberList);
        vo.setSpecialNeedsList(specialNeedsList);

        return vo;
    }

    /**
     * 获取配件模具字典信息
     * @return 配件模具字典信息
     */
    public DictionaryCategoryVo getPartsDictionaryCategory(){
        //初始化查询对象
        DictionaryEntity req = new DictionaryEntity();
        //大类
        req.setType(DictionaryTypeEnum.COMMODITY_INFO.getId());
        //数据有效
        req.setValid(DataIsValidEnum.VALID.getId());
        //查询结果
        List<DictionaryEntity> dictionaryList = dictionaryService.queryAll(req);
        //初始化回参对象
        DictionaryCategoryVo vo = new DictionaryCategoryVo();
        if(CollectionUtils.isEmpty(dictionaryList)){
            return vo;
        }
        //垫片种类编码
        List<DictionaryEntity> categoryList = new ArrayList<>();

        //垫片种类
        List<DictionaryEntity> moldCategoryList = new ArrayList<>();

        //垫片耳朵
        List<DictionaryEntity> firstCategoryList = new ArrayList<>();

        //垫片特殊参数
        List<DictionaryEntity> secondaryCategoryList = new ArrayList<>();

        //垫片尺寸
        List<DictionaryEntity> threeLevelCategoryList = new ArrayList<>();

        //印刷稿件3位简码
        List<DictionaryEntity> craftList = new ArrayList<>();

        //特殊需求
        List<DictionaryEntity> specialNeedsList = new ArrayList<>();

        for(DictionaryEntity dictionary : dictionaryList){
            Integer lx = dictionary.getLx();
            if(null == lx){
                continue;
            }
            switch (lx){
                case 68:
                    categoryList.add(dictionary);
                    break;
                case 69:
                    moldCategoryList.add(dictionary);
                    break;
                case 70:
                    firstCategoryList.add(dictionary);
                    break;
                case 71:
                    secondaryCategoryList.add(dictionary);
                    break;
                case 72:
                    threeLevelCategoryList.add(dictionary);
                    break;
                case 73:
                    craftList.add(dictionary);
                    break;
                case 88:
                    specialNeedsList.add(dictionary);
                    break;
            }
        }

        vo.setCategoryList(categoryList);
        vo.setMoldCategoryList(moldCategoryList);
        vo.setFirstCategoryList(firstCategoryList);
        vo.setSecondaryCategoryList(secondaryCategoryList);
        vo.setThreeLevelCategoryList(threeLevelCategoryList);
        vo.setCraftList(craftList);
        vo.setSpecialNeedsList(specialNeedsList);

        return vo;
    }


    /**
     * 获取规格配件信息
     *
     * @param req 查询条件
     * @return 规格配件信息
     */
    public List<ProductInfoRsp> getSpecPartsInfoByConcat(ProductInfoReq req) {
        //配件
        req.setProductType(2);
        //调用接口查询结果
        List<ProductInfoRsp> productInfoList = stockService.getSpecPartsInfoByConcat(req);

        return productInfoList;
    }

    /**
     * 获取规格配件信息
     *
     * @param req 查询条件
     * @return 规格配件信息
     */
    public List<ProductInfoRsp> getSpecPartsInfo(ProductInfoReq req) {
        //配件
        req.setProductTypeStr("2,3,4");
        //调用接口查询结果
        List<ProductInfoRsp> productInfoList = stockService.getSpecPartsInfoByConcat(req);

        return productInfoList;
    }

    /**
     * 获取产品信息
     *
     * @param req 查询条件
     * @return 产品信息
     */
    public List<ProductInfoRsp> getProductInfoByConcat(ProductInfoReq req) {
        //产品
        req.setProductType(1);
        //调用接口查询结果
        List<ProductInfoRsp> productInfoList = stockService.getSpecPartsInfoByConcat(req);

        return productInfoList;
    }

    /**
     * 字典列表-导出
     * @param lx 小类
     * @return 字典列表-导出
     */
    public JSONObject productDictionaryExport(Integer lx) {
        JSONObject obj = new JSONObject();
        //获取excel的title信息
        JSONArray titleList = getTitleInfo(lx);
        JSONArray data = new JSONArray();
        data.add(titleList);
        if (null == lx) {
            obj.put("data", data);
            return obj;
        }
        getDictionaryInfo(data, lx);
        obj.put("data", data);
        return obj;
    }

    /**
     * 处理导出的数据
     *
     * @param data 处理后的数据
     * @param lx   小类
     */
    private void getDictionaryInfo(JSONArray data, Integer lx) {
        //初始化查询对象
        DictionaryEntity req = new DictionaryEntity();
        //大类
        req.setType(DictionaryTypeEnum.COMMODITY_INFO.getId());
        //小类
        req.setLx(lx);
        //数据有效
        req.setValid(DataIsValidEnum.VALID.getId());
        //查询结果
        List<DictionaryEntity> dictionaryList = dictionaryService.queryAll(req);
        if (CollectionUtils.isEmpty(dictionaryList)) {
            return;
        }
        //包材和配件的工艺需要导出英文简码
        boolean flag = lx.equals(DictionaryLxEnum.CRAFT_CATEGORY.getId()) || lx.equals(DictionaryLxEnum.PARTS_CRAFT_CATEGORY.getId());
        for (DictionaryEntity dictionary : dictionaryList) {
            JSONArray infoList = new JSONArray();
            //编码
            infoList.add(getNotNullOfStringInfo(dictionary.getCode()));
            if (flag) {
                //英文简码
                infoList.add(getNotNullOfStringInfo(dictionary.getCountry()));
            }
            //名称
            infoList.add(getNotNullOfStringInfo(dictionary.getName()));
            //是否停用(1:否 0:是)
            infoList.add(dictionary.getValid().equals(DataIsValidEnum.VALID.getId()) ? "否" : "是");
            //中文描述
            infoList.add(getNotNullOfStringInfo(dictionary.getRemark()));
            //英文描述
            infoList.add(getNotNullOfStringInfo(dictionary.getEnglishname()));

            data.add(infoList);
        }
    }

    /**
     * 获取String类型不为空数据
     * @param value 目标数据
     * @return 不为空数据
     */
    private String getNotNullOfStringInfo(String value){
        if(StringUtils.isEmpty(value)){
            return "";
        }else {
            return value;
        }
    }

    /**
     * 获取excel的title信息
     * @param lx 字典小类
     * @return excel的title信息
     */
    public JSONArray getTitleInfo(Integer lx) {
        JSONArray obj = new JSONArray();
        obj.add("编码");
        boolean flag = lx.equals(DictionaryLxEnum.CRAFT_CATEGORY.getId()) || lx.equals(DictionaryLxEnum.PARTS_CRAFT_CATEGORY.getId());
        if(null != lx && flag){
            obj.add("英文简码");
        }
        obj.add("名称");
        obj.add("是否停用");
        obj.add("中文描述");
        obj.add("英文描述");
        return obj;
    }

}
